package utils;

import java.util.Arrays;
import java.util.HashMap;

public class SpikePatternGenerator {

	private int patternDuration;
	@SuppressWarnings("unused")
	private short numberOfPatterns;
	private short spikeChannels;
	private float spikeProbability;
	private HashMap<Short, short [][]> patternMap;
	
	/**
	 * 
	 * @param patternDuration
	 * @param spikeChannels
	 * @param spikeProbability
	 */
	public SpikePatternGenerator(int patternDuration, short spikeChannels,
			float spikeProbability) {
		this.patternDuration = patternDuration;
		this.spikeChannels = spikeChannels;
		this.spikeProbability = spikeProbability;
	}
	
	/**
	 * 
	 * @return
	 */
	public HashMap<Short, short [][]> generateRandomPatterns(short numberOfPatterns){
		this.numberOfPatterns = numberOfPatterns;
		short [][] pattern;
		GrowableShortArray epochIndices;
		
		for(short p = 0; p < numberOfPatterns; p++){
			pattern = new short[patternDuration][];
			
			for(int e = 0; e < patternDuration; e++){		
				epochIndices = new GrowableShortArray();
				
				for(short c = 0; c < spikeChannels; c++){
					
					if(Math.random() < spikeProbability){
						epochIndices.add(c);
					}
					
				}
				pattern[e] = epochIndices.getArray();
			}
			patternMap.put(p, pattern);
		}
		
		return patternMap;
		
	}
	
//	public HashMap<Short, short[][]>  generateJitteredPatterns(
//			short numPrototypes, short numJitteredPatterns, float jitterProbability) {
//		this.numberOfPatterns = (short) (numPrototypes * numJitteredPatterns);
//		
//		
//	}
	
	/**
	 * 
	 * @author zach
	 *
	 */
	private static class GrowableShortArray {
		
		private static final int DEFAULT_CAPACITY = 50;
		private int size;
		private short [] underlyingArray = new short[DEFAULT_CAPACITY];		
		
		public GrowableShortArray(){
			size = 0;
		}
		
		public void add(short i) {
			
			if(size >= underlyingArray.length * 0.75) {
				short [] grow = new short[underlyingArray.length * 2];
				for(int j = 0, n = underlyingArray.length; j < n; j++){
					grow[j] = underlyingArray[j];
				}
				underlyingArray = grow;
			}
			
			size++;
			underlyingArray[size - 1] = i;
			
		}
	
		public short[] getArray(){
			return Arrays.copyOfRange(underlyingArray, 0, size);	
		}
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
